package com.ymttest.business.service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.Map;

import org.json.JSONException;

import com.ymatou.iapi.shoppingcartservice.parameter.ShoppingcartProductBean;
import com.ymatou.iapi.shoppingcartservice.service.AddShoppingCartCall;
import com.ymttest.database.mongo.YmtShoppingCartWapper;
import com.ymttest.utils.MapUtil;
import com.ymttest.utils.compare.AssertHelper;

public class BuyerShoppingCartService {
	
	private static AddShoppingCartCall addShoppingCartCall = new AddShoppingCartCall();
	YmtShoppingCartWapper ymtShoppingCartWapper = new YmtShoppingCartWapper();
	
	/**
	 * 添加商品
	 * 
	 * @return String
	 * @throws JSONException 
	 * @throws Exception
	 */
	public String AddShoppingCartProduct(int userId) throws JSONException, Exception {
		ShoppingcartProductBean shoppingcartProductBean = ShoppingcartProductBean.getAddShoppingCartRequestFromFile("addshoppingcartcalltpl", 
				MapUtil.hashMap("userId", userId));
		
		addShoppingCartCall.callAndGetReturnData(shoppingcartProductBean);
		Map<String,Object> shoppingcartProductDB = ymtShoppingCartWapper.getByProductIdAndUserId
				(shoppingcartProductBean.getProductId(), userId);
		
		return String.valueOf(shoppingcartProductDB.get("_id"));
	}
	
	/**
	 * 添加商品
	 * 
	 * @return String
	 * @throws JSONException 
	 * @throws Exception
	 */
	public String AddShoppingCartProduct(int userId, String catalogId) throws JSONException, Exception {
		ShoppingcartProductBean shoppingcartProductBean = ShoppingcartProductBean.getAddShoppingCartRequestFromFile("addshoppingcartcalltpl", 
				MapUtil.hashMap("userId", userId,
								"catalogId", catalogId));
		
		addShoppingCartCall.callAndGetReturnData(shoppingcartProductBean);
		Map<String,Object> shoppingcartProductDB = ymtShoppingCartWapper.getByCatalogIdIdAndUserId
				(shoppingcartProductBean.getCatalogId(), userId);
		
		return String.valueOf(shoppingcartProductDB.get("_id"));
	}
	
	/**
	 * 添加商品
	 * 
	 * @return String
	 * @throws JSONException 
	 * @throws Exception
	 */
	public String AddShoppingCartProduct(int userId, int source) throws JSONException, Exception {
		ShoppingcartProductBean shoppingcartProductBean = ShoppingcartProductBean.getAddShoppingCartRequestFromFile("addshoppingcartcalltpl", 
				MapUtil.hashMap("userId", userId,
								"source", source));
		
		addShoppingCartCall.callAndGetReturnData(shoppingcartProductBean);
		Map<String,Object> shoppingcartProductDB = ymtShoppingCartWapper.getByCatalogIdIdAndUserId
				(shoppingcartProductBean.getCatalogId(), userId);
		
		return String.valueOf(shoppingcartProductDB.get("_id"));
	}
	
	/**
	 * 根据userid获取购物车mongo数据
	 * 
	 * @return String
	 * @throws JSONException 
	 * @throws Exception
	 */
	public Map<String,Object> getShoppingCartProduct(int userId) throws JSONException, Exception {
		ShoppingcartProductBean shoppingcartProductBean = ShoppingcartProductBean.getAddShoppingCartRequestFromFile("addshoppingcartcalltpl", 
				MapUtil.hashMap("userId", userId));
		
		addShoppingCartCall.callAndGetReturnData(shoppingcartProductBean);
		Map<String,Object> shoppingcartProductDB = ymtShoppingCartWapper.getByProductIdAndUserId
				(shoppingcartProductBean.getProductId(), userId);
		
		return shoppingcartProductDB;
	}
	
	/**
	 * 根据userid和source获取购物车mongo数据
	 * 
	 * @return String
	 * @throws JSONException 
	 * @throws Exception
	 */
	public Map<String,Object> getShoppingCartProduct(int userId, String catalogId) throws JSONException, Exception {
		ShoppingcartProductBean shoppingcartProductBean = ShoppingcartProductBean.getAddShoppingCartRequestFromFile("addshoppingcartcalltpl", 
				MapUtil.hashMap("userId", userId,
								"catalogId", catalogId));
		
		addShoppingCartCall.callAndGetReturnData(shoppingcartProductBean);
		Map<String,Object> shoppingcartProductDB = ymtShoppingCartWapper.getByProductIdAndUserId
				(shoppingcartProductBean.getProductId(), userId);
		
		return shoppingcartProductDB;
	}
	
	/**
	 * 根据userid和source获取购物车mongo数据
	 * 
	 * @return String
	 * @throws JSONException 
	 * @throws Exception
	 */
	public Map<String,Object> getShoppingCartProduct(int userId, int source) throws JSONException, Exception {
		ShoppingcartProductBean shoppingcartProductBean = ShoppingcartProductBean.getAddShoppingCartRequestFromFile("addshoppingcartcalltpl", 
				MapUtil.hashMap("userId", userId,
								"source", source));
		
		addShoppingCartCall.callAndGetReturnData(shoppingcartProductBean);
		Map<String,Object> shoppingcartProductDB = ymtShoppingCartWapper.getByProductIdAndUserId
				(shoppingcartProductBean.getProductId(), userId);
		
		return shoppingcartProductDB;
	}
	
	public void checkShoppingcartProductBeanFromDB(ShoppingcartProductBean reqBean) throws Exception {
		Map<String,Object> shoppingcartProductDB = ymtShoppingCartWapper.getByCatalogIdIdAndUserId(reqBean.getCatalogId(), reqBean.getUserId()); 
		
		ShoppingcartProductBean actBean = new ShoppingcartProductBean();
		actBean.setShoppingCartId(shoppingcartProductDB.get("_id").toString());
		actBean.setCreateTime(shoppingcartProductDB.get("createtime").toString());
		actBean.setLastModifiedTime(this.CSTToDate(shoppingcartProductDB.get("lastmodifiedtime").toString()));
		actBean.setEndTime(this.CSTToDate(shoppingcartProductDB.get("endtime").toString()));
		actBean.setUserId(Integer.parseInt(shoppingcartProductDB.get("userid").toString()));
		actBean.setSellerId(Integer.parseInt(shoppingcartProductDB.get("sellerid").toString()));
		actBean.setCatalogId(shoppingcartProductDB.get("catalogid").toString());
		actBean.setProductId(shoppingcartProductDB.get("productid").toString());
		actBean.setNum(Integer.parseInt(shoppingcartProductDB.get("num").toString()));
		actBean.setStatus(Integer.parseInt(shoppingcartProductDB.get("status").toString()));
		actBean.setSource(Integer.parseInt(shoppingcartProductDB.get("source").toString()));
		actBean.setClient(Integer.parseInt(shoppingcartProductDB.get("client").toString()));
		actBean.setMainOrderId(shoppingcartProductDB.get("mainorderid").toString());
		actBean.setProductName(shoppingcartProductDB.get("productname").toString());
		actBean.setDeliveryType(Integer.parseInt(shoppingcartProductDB.get("deliverytype").toString()));
		actBean.setTariffType(Integer.parseInt(shoppingcartProductDB.get("tarifftype").toString()));
		actBean.setSkuInfo(shoppingcartProductDB.get("skuinfo").toString());
		actBean.setPrice(Double.parseDouble(shoppingcartProductDB.get("price").toString()));
		actBean.setPic(shoppingcartProductDB.get("pic").toString());
		actBean.setProductType(Integer.parseInt(shoppingcartProductDB.get("producttype").toString()));
		actBean.setRefundType(Integer.parseInt(shoppingcartProductDB.get("refundtype").toString()));
		actBean.setValidEnd(this.CSTToDate(shoppingcartProductDB.get("validend").toString()));
		actBean.setValidStart(this.CSTToDate(shoppingcartProductDB.get("validstart").toString()));
		actBean.setSellerName(shoppingcartProductDB.get("sellername").toString());
		actBean.setSellerLogo(shoppingcartProductDB.get("sellerlogo").toString());
		actBean.setSellerCountry(shoppingcartProductDB.get("sellercountry").toString());
		actBean.setSellerFlag(shoppingcartProductDB.get("sellerflag").toString());
		actBean.setActivityId(Integer.parseInt(shoppingcartProductDB.get("activityid").toString()));
		actBean.setNoReasonReturn(Boolean.parseBoolean(shoppingcartProductDB.get("isnoreasonreturn").toString()));
		actBean.setActivityName(shoppingcartProductDB.get("activityname").toString());
		actBean.setSingleCatalog(Boolean.parseBoolean(shoppingcartProductDB.get("issinglecatalog").toString()));
		actBean.setVersion(shoppingcartProductDB.get("version").toString());
		actBean.setAddPrice(Double.parseDouble(shoppingcartProductDB.get("addprice").toString()));
		actBean.setStock(Integer.parseInt(shoppingcartProductDB.get("stock").toString()));
		
		AssertHelper.assertResultEqual(reqBean, actBean, "");
	}
	
	public String CSTToDate(String CSTString) throws ParseException { 
		
		SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);  
		Date d = sdf.parse(CSTString);  
		String formatDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(d);  
		
		return formatDate;
	}
	
	public static void main(String[] args) throws JSONException, Exception {
		BuyerShoppingCartService service = new BuyerShoppingCartService();
		service.AddShoppingCartProduct(32142, "catalogId1");
		service.AddShoppingCartProduct(32142, "catalogId2");
	}
}
